package ru.spbau.lifeincube.gui3d;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;

public class Cube extends Group {
    final Rotate rx = new Rotate(0, Rotate.X_AXIS);
    final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
    final Rotate rz = new Rotate(0, Rotate.Z_AXIS);
    private final Rectangle frontFace;
    private final Rectangle topFace;
    private final Rectangle leftFace;
    private final Rectangle rightFace;
    private final Rectangle bottomFace;
    private final Rectangle backFace;
	private double shade;

	private ObjectProperty<Color> color = new SimpleObjectProperty<Color>(this, "color", Color.RED) {
		@Override
		public void setValue(Color c) {
			frontFace.setFill(c);
			topFace.setFill(c.deriveColor(0.0, 1.0, (1 - 0.1 * shade), 1.0));
			leftFace.setFill(c.deriveColor(0.0, 1.0, (1 - 0.2 * shade), 1.0));
			rightFace.setFill(c.deriveColor(0.0, 1.0, (1 - 0.3 * shade), 1.0));
			bottomFace.setFill(c.deriveColor(0.0, 1.0, (1 - 0.4 * shade), 1.0));
			backFace.setFill(c.deriveColor(0.0, 1.0, (1 - 0.5 * shade), 1.0));
		}
	};

	public final Color getColor() { return color.get(); }
	public final void setColor(Color c) { color.setValue(c); }
	public ReadOnlyObjectProperty<Color> colorProperty() { return color; }

	public Cube(double size, Color color, double shade) {
		this.shade = shade;
		getTransforms().addAll(rz, ry, rx);
		backFace = RectangleBuilder.create() // back face
				.width(size).height(size)
				.fill(color.deriveColor(0.0, 1.0, (1 - 0.5 * shade), 1.0))
				.translateX(-0.5 * size)
				.translateY(-0.5 * size)
				.translateZ(0.5 * size)
				.build();
		bottomFace = RectangleBuilder.create() // bottom face
				.width(size).height(size)
				.fill(color.deriveColor(0.0, 1.0, (1 - 0.4 * shade), 1.0))
				.translateX(-0.5 * size)
				.translateY(0)
				.rotationAxis(Rotate.X_AXIS)
				.rotate(90)
				.build();
		rightFace = RectangleBuilder.create() // right face
				.width(size).height(size)
				.fill(color.deriveColor(0.0, 1.0, (1 - 0.3 * shade), 1.0))
				.translateX(-1 * size)
				.translateY(-0.5 * size)
				.rotationAxis(Rotate.Y_AXIS)
				.rotate(90)
				.build();
		leftFace = RectangleBuilder.create() // left face
				.width(size).height(size)
				.fill(color.deriveColor(0.0, 1.0, (1 - 0.2 * shade), 1.0))
				.translateX(0)
				.translateY(-0.5 * size)
				.rotationAxis(Rotate.Y_AXIS)
				.rotate(90)
				.build();
		topFace = RectangleBuilder.create() // top face
				.width(size).height(size)
				.fill(color.deriveColor(0.0, 1.0, (1 - 0.1 * shade), 1.0))
				.translateX(-0.5 * size)
				.translateY(-1 * size)
				.rotationAxis(Rotate.X_AXIS)
				.rotate(90)
				.build();
		frontFace = RectangleBuilder.create() // front face
				.width(size).height(size)
				.fill(color)
				.translateX(-0.5 * size)
				.translateY(-0.5 * size)
				.translateZ(-0.5 * size)
				.build();
		getChildren().addAll(
				backFace,
				bottomFace,
				rightFace,
				leftFace,
				topFace,
				frontFace
		);
	}
/*
	public void setColor(Color c) {

	}*/
}
